$(".table").on("click", "tbody tr", function() {
	var obj = $(this);
	if(obj.hasClass("success")) {
		obj.removeClass("success")
	} else {
		obj.parent().children().removeClass("success");
		obj.addClass("success")
	}
});

$("input[name=id]").change(() => handleTips(""));

function initBtn(id, icon) {
	var img = "<img title=icon src='/"+icon+"'\>"
	var edit = '<button class="btn btn-info btn-edit" data-id="'+id+'">Edit</button> '
	var pic = '<button class="btn btn-success" tabindex="0" role="button" data-toggle="popover" data-trigger="hover" data-content="'+img+'">Pic</button> '
	var del = '<button class="btn btn-primary btn-del" data-id="'+id+'">Delete</button>'
	
	return edit+pic+del;
}

$(".table thead tr th").not(":last").click(function() {
	var sortById = function(i, isAsc) {
		var tbody = $(".table tbody");
		var trs = tbody.children();
		if(isAsc) {
			trs.sort((a, b) => $(a).children().eq(i).text() >= $(b).children().eq(i).text());
		} else {
			trs.sort((a, b) => $(a).children().eq(i).text() < $(b).children().eq(i).text());
		}
		tbody.html(trs);
	}

	var isAse = $(this).data("ase") == true ? true : false;
	sortById($(this).index(), isAse);
	$(this).data("ase", !isAse);
	// handle caret
	$(this).siblings().children(".caret").addClass("reverse");
	if(isAse) {
		$(this).children(".caret").addClass("reverse");

	} else {
		$(this).children(".caret").removeClass("reverse");
	}
});

function __resetTablePopover() {
	$('[data-toggle="popover"]').popover({
		"html": true,
		"template": '<div class="popover" role="tooltip"><div class="arrow"></div><div class="popover-content popover-user-icon"></div></div>'
	})
}